package initialize

import (
	"fmt"
	"ginBasic/global"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var err error

func InitDB() {
	ip := Config("mysql.ip")
	port := Config("mysql.port")
	user := Config("mysql.user")
	password := Config("mysql.password")
	database := Config("mysql.database")

	// dsn := "root:123456@tcp(192.168.0.6:3306)/gin?charset=utf8mb4&parseTime=True&loc=Local"
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", user, password, ip, port, database)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		QueryFields: true, //打印sql
		//SkipDefaultTransaction: true, //禁用事务
	})
	// DB.Debug()
	if err != nil {
		panic(err)
	}
}
